package com.bw.paper.mvp.view.video;

import com.bw.paper.R;
import com.bw.paper.mvp.base.model.BaseEntity;
import com.bw.paper.mvp.base.view.BaseFragment;
import com.bw.paper.mvp.contract.LREContract;
import com.bw.paper.mvp.di.DaggerLREComponent;
import com.bw.paper.mvp.di.LREModules;
import com.bw.paper.mvp.model.HttpCode;
import com.bw.paper.mvp.model.entity.VideoNewsEntity;
import com.bw.paper.mvp.presenter.LREPresenter;
import com.bw.paper.mvp.view.video.adapter.SmallVideoAdapter;
import com.bw.paper.utils.ToastUtils;
import com.bw.paper.widget.VideoNewsItemDecoration;
import com.scwang.smart.refresh.footer.ClassicsFooter;
import com.scwang.smart.refresh.header.ClassicsHeader;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener;

import java.util.HashMap;
import java.util.Map;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

public class SmallVideoFragment extends BaseFragment<LREPresenter>implements
        LREContract.ILREView, OnRefreshLoadMoreListener {

    private SmartRefreshLayout small_smart;
    private RecyclerView small_rv;
    private GridLayoutManager manager;
    private SmallVideoAdapter adapter;

    @Override
    public int bindLayout() {
        return R.layout.fragment_small_video;
    }

    @Override
    public void initView() {
        small_smart = f(R.id.small_smart);
        small_rv = f(R.id.small_rv);
        small_smart.setEnableRefresh(true);
        small_smart.setEnableLoadMore(true);
        small_smart.setRefreshHeader(new ClassicsHeader(getContext()));
        small_smart.setRefreshFooter(new ClassicsFooter(getContext()));
        small_smart.setOnRefreshLoadMoreListener(this);
        manager = new GridLayoutManager(getContext(),2);
        manager.setOrientation(GridLayoutManager.VERTICAL);
        small_rv.setLayoutManager(manager);
        small_rv.addItemDecoration(new VideoNewsItemDecoration(getContext()));
        adapter = new SmallVideoAdapter();
        small_rv.setAdapter(adapter);
    }

    @Override
    public void initData() {
        Map<String,Object>map = new HashMap<>();
        map.put("news_type",0);
        map.put("code", HttpCode.VIDEONEWSCODE);
        p.requestAll(map);
    }

    @Override
    public void inject() {
        DaggerLREComponent.builder().lREModules(new LREModules(this))
                .build().injectSmallVideoFragment(this);
    }

    @Override
    public void refreshAll(BaseEntity entity) {
        VideoNewsEntity videoNewsEntity = (VideoNewsEntity) entity;
        if (videoNewsEntity.getValues()==null){
            ToastUtils.showMsg("请求出错");
            return;
        }
        adapter.setNewInstance(videoNewsEntity.getValues());
    }

    @Override
    public void refreshRecyckerView(BaseEntity entity) {
        VideoNewsEntity videoNewsEntity = (VideoNewsEntity) entity;
        if (videoNewsEntity.getValues()==null){
            ToastUtils.showMsg("请求出错");
            return;
        }
        adapter.setNewInstance(videoNewsEntity.getValues());
    }

    @Override
    public void loadMoreRecyclerView(BaseEntity entity) {
        VideoNewsEntity videoNewsEntity = (VideoNewsEntity) entity;
        if (videoNewsEntity.getValues()==null){
            ToastUtils.showMsg("最后一页");
            return;
        }
        adapter.addData(videoNewsEntity.getValues());
    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        refreshLayout.finishRefresh();
        Map<String,Object>map = new HashMap<>();
        map.put("news_type",0);
        map.put("code", HttpCode.VIDEONEWSCODE);
        p.requestLoad(map);
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        refreshLayout.finishRefresh();
        Map<String,Object>map = new HashMap<>();
        map.put("news_type",0);
        map.put("code", HttpCode.VIDEONEWSCODE);
        p.requestRefresh(map);
    }
}
